package com.ruoyi.web.controller.gas;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.gas.domain.GasWarnRectify;
import com.ruoyi.gas.service.*;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName 小程序接口
 * @Description TODO
 * @Author ZYJ
 * @Date 2023/12/22 9:21
 * @Version 1.0
 **/
@RestController
@RequestMapping("/gas/applet")
public class AppletController extends BaseController{
    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IGasEventDictService gasEventDictService;

    @Autowired
    private IGasDetectionObjectDictService gasDetectionObjectDictService;

    @Autowired
    private IGasDataUnitDictService gasDataUnitDictService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IGasWarnRectifyService gasWarnRectifyService;

    /**
     * 获取安全负责人所在公司的所有安全事件列表
     * @return
     */
    @GetMapping("/getAllEventListByHead")
    public TableDataInfo getAllEventListByHead(){
        startPage();
        SysDept sysDept = sysDeptService.selectDeptById(SecurityUtils.getDeptId());
        List<Map<String, Object>> list = gasWarnRectifyService.getAllWarnListByHead(sysDept.getCompanyId());
        return getDataTable(list);
    }

    /**
     * 获取事件详情
     * @param id 事件id
     * @return
     */
    @GetMapping("/getEventDetailById")
    public AjaxResult getEventDetailById(Long id){
        return AjaxResult.success(gasWarnRectifyService.getWarnDetailById(id));
    }

    /**
     * 安全负责人获取整改人列表
     * @param deptId 安全负责人所在部门id
     * @return
     */
    @GetMapping("/getGeneralUserList")
    public AjaxResult getGeneralUserList(Long deptId){
        return AjaxResult.success(userService.getGeneralUserList(deptId));
    }

    /**
     * 安全负责人分配整改人
     * @param gasWarnRectify 操作记录
     * @return
     */
    @PostMapping("/divideEventRectify")
    public AjaxResult divideEventRectify(@RequestBody GasWarnRectify gasWarnRectify){
        gasWarnRectify.setShareTime(DateUtils.getNowDate());
        gasWarnRectify.setStatus(1);
        return AjaxResult.success(gasWarnRectifyService.updateGasWarnRectify(gasWarnRectify));
    }

    /**
     * 整改人获取分配给自己的待整改事件列表
     * @return
     */
    @GetMapping("/getEventListByRectifier")
    public TableDataInfo getEventListByRectifier(){
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = gasWarnRectifyService.getWarnListByRectifier(userId);
        return getDataTable(list);
    }

    /**
     * 整改人上传整改证明
     * @param file
     * @return
     * @throws IOException
     * @throws InvalidExtensionException
     */
    @PostMapping("/uploadRectifyUrl")
    public AjaxResult uploadRectifyUrl(@RequestParam MultipartFile file) throws IOException, InvalidExtensionException {
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            return AjaxResult.success("上传成功",avatar);
        }else {
            return AjaxResult.error("上传失败");
        }
    }

    /**
     * 整改人整改
     * @param gasWarnRectify 整改记录
     * @return
     */
    @PutMapping("/putRectifyEvent")
    public AjaxResult putRectifyEvent(@RequestBody GasWarnRectify gasWarnRectify){
        //已整改
        gasWarnRectify.setStatus(2);
        gasWarnRectify.setRectifyTime(DateUtils.getNowDate());
        return toAjax(gasWarnRectifyService.updateGasWarnRectify(gasWarnRectify));
    }


    /**
     * 安全负责人获取已整改事件列表
     * @return
     */
    @GetMapping("/getAuditEventList")
    public AjaxResult getAuditEventList(){
        Long userId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = gasWarnRectifyService.getAuditWarnList(userId);
        return AjaxResult.success(list);
    }

    /**
     * 安全负责人查看已整改事件详情
     * @param id 整改记录id
     * @return
     */
    @GetMapping("/getRectifyDetail")
    public AjaxResult getRectifyDetail(Long id){
        List<Map<String,Object>> list = gasWarnRectifyService.getDetailListByWarnId(id);
        return AjaxResult.success(list);
    }


    /**
     * 安全负责人审核已整改事件
     * @return
     */
    @PutMapping("/processEvent")
    private AjaxResult processEvent(@RequestBody  GasWarnRectify gasWarnRectify){
        gasWarnRectify.setStatus(3);
        gasWarnRectify.setProcessTime(DateUtils.getNowDate());
        return AjaxResult.success(gasWarnRectifyService.updateGasWarnRectify(gasWarnRectify));
    }


    /**
     * 获取安全事件字典列表
     * @return
     */
    @GetMapping("/getEventDictList")
    public AjaxResult getEventDictList(){
        return AjaxResult.success(gasEventDictService.selectGasEventDictList(null));
    }

    /**
     * 获取检测对象字典列表
     * @return
     */
    @GetMapping("/getDetectionObjectDictList")
    public AjaxResult getDetectionObjectDictList(){
        return AjaxResult.success(gasDetectionObjectDictService.selectGasDetectionObjectDictList(null));
    }

    /**
     * 获取数据单位字典列表
     * @return
     */
    @GetMapping("/getDataUnitDictList")
    public AjaxResult getDataUnitDictList(){
        return AjaxResult.success(gasDataUnitDictService.selectGasDataUnitDictList(null));
    }
}
